﻿using System.Configuration;
using System.IO;
using NUnit.Framework;
using Raven.Client;

namespace kinokasse.belegungenrepo.tests.DocumentStoreFactoryTests
{
    [TestFixture]
    public class Soll_neuen_DocumentStore_initialisieren
    {
        [Test]
        public void Wenn_kein_ConfigurationsEintrag_angegeben_wurde()
        {
            RemoveConnectionString();
            DeleteDataDirectory(belegungenrepo.DocumentStoreFactory.DefaultDataDirectory);

            IDocumentStore documentStore = null;

            Assert.DoesNotThrow(() => documentStore = belegungenrepo.DocumentStoreFactory.InitializeDocumentStore());
            Assert.IsTrue(Directory.Exists(belegungenrepo.DocumentStoreFactory.DefaultDataDirectory));
            Assert.IsNotNull(documentStore);

            documentStore.Dispose();
        }


        [Test]
        public void Wenn_ein_File_Uri_angegeben_wurde()
        {
            string connectionStringValue = Path.GetFullPath(@".\DB");

            TestConfigurationHelper.SetConnectionString(connectionStringValue);
            DeleteDataDirectory(connectionStringValue);

            IDocumentStore documentStore = null;

            Assert.DoesNotThrow(() => documentStore = belegungenrepo.DocumentStoreFactory.InitializeDocumentStore());
            Assert.IsTrue(Directory.Exists(connectionStringValue));
            Assert.IsNotNull(documentStore);

            documentStore.Dispose();
        }

        [Test]
        public void Wenn_ein_relative_File_Uri_angegeben_wurde()
        {
            const string connectionStringValue = @".\DB";

            TestConfigurationHelper.SetConnectionString(connectionStringValue);
            DeleteDataDirectory(connectionStringValue);

            IDocumentStore documentStore = null;

            Assert.DoesNotThrow(() => documentStore = belegungenrepo.DocumentStoreFactory.InitializeDocumentStore());
            Assert.IsTrue(Directory.Exists(connectionStringValue));
            Assert.IsNotNull(documentStore);

            documentStore.Dispose();
        }

        [Test]
        public void Wenn_ein_http_Uri_angegeben_wurde()
        {
            TestConfigurationHelper.SetConnectionString(@"http://localhost:8080");

            IDocumentStore documentStore = null;

            Assert.DoesNotThrow(() => documentStore = belegungenrepo.DocumentStoreFactory.InitializeDocumentStore());
            Assert.IsNotNull(documentStore);

            documentStore.Dispose();
        }

        #region "Helper"

        private void RemoveConnectionString()
        {
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var connectionString = config.ConnectionStrings.ConnectionStrings[(string) DocumentStoreFactory.ConnectionStringName];

            if (connectionString != null)
                config.ConnectionStrings.ConnectionStrings.Remove(
                    new ConnectionStringSettings(belegungenrepo.DocumentStoreFactory.ConnectionStringName, ""));

            config.Save();

            ConfigurationManager.RefreshSection("connectionStrings");

        }

        private void DeleteDataDirectory(string dataDirectory)
        {
            if (Directory.Exists(dataDirectory))
                Directory.Delete(dataDirectory, true);
        }
        #endregion
    }
}
